package cn.psvmc.cxzapi.controller.market;

import cn.psvmc.cxzapi.model.common.DelEntity;
import cn.psvmc.cxzapi.model.common.MarketEntity;
import cn.psvmc.cxzapi.model.market.TSortComponent;
import cn.psvmc.cxzapi.response.Result;
import cn.psvmc.cxzapi.response.ResultGenerator;
import cn.psvmc.cxzapi.service.market.TSortComponentService;
import cn.psvmc.cxzapi.util.ToolUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 分类推荐组件实例管理
 * @Author auto
 * @Date 2024-08-13
 */
@RestController
@RequestMapping("/component")
public class TSortComponentController {

    @Resource
    private TSortComponentService tSortComponentService;

    /**
    * @Author auto 2024-08-13 新增分类推荐组件实例
    * @Param tSortComponent 分类推荐组件实例
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/add")
    public Result add(@RequestBody TSortComponent tSortComponent) {
        int addResult = tSortComponentService.add(tSortComponent);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
    * @Author auto 2024-08-13 更新分类推荐组件实例
    * @Param tSortComponent 分类推荐组件实例模型
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/edit")
    public Result edit(@RequestBody TSortComponent tSortComponent) {
        ToolUtils.validation(new Object[]{tSortComponent.getScid(), "id", 1, tSortComponent.getRversion(), "版本", 1});
        int updateResult = tSortComponentService.edit(tSortComponent);
        if (updateResult > 0) {
            return ResultGenerator.genSuccessResult("更新成功！");
        }
        return ResultGenerator.genFailResult("更新失败！");
    }

    /**
    * @Author auto 2024-08-13 删除分类推荐组件实例
    * @Param [entity] 删除实体类
    * @Return cn.psvmc.cxzapi.response.Result
    */
    @PostMapping("/delete")
    public Result delete(@RequestBody DelEntity entity) {
        Integer id = entity.getId();
        Integer rversion = entity.getRversion();
        ToolUtils.validation(new Object[]{id, "id", 1, rversion, "版本", 1});
        int delResult = tSortComponentService.delete(id, rversion);
        if (delResult > 0) {
            return ResultGenerator.genSuccessResult("删除成功！");
        }
        return ResultGenerator.genFailResult("删除失败！");
    }

    /**
     * @Description 列表
     * @Date 2024/8/13 16:55
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list")
    public Result list(@RequestBody MarketEntity entity) {
        return ResultGenerator.genSuccessResult("查询成功！", tSortComponentService.listAll(entity));
    }

    /**
     * @Description 删除分组
     * @Date 2024/8/18 16:16
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/delete_group")
    public Result deleteGroup(@RequestBody MarketEntity entity) {
        String groupName = entity.getGroupName();
        String componentId = entity.getComponentId();
        ToolUtils.validation(new Object[]{groupName, "分组名", 2, componentId, "组件id", 2});
        tSortComponentService.deleteGroup(groupName, componentId);
        return ResultGenerator.genSuccessResult("删除成功！");
    }
}
